//
//  SportsViewController.m
//  RoadKnowEntertainment
//
//  Created by lanou on 16/8/6.
//  Copyright © 2016年 RKE. All rights reserved.
//

#import "SportsViewController.h"
#import "TopStoriesModel.h"
#import "NormalTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "PhotosetCell.h"
#import "DYMRollingBannerVC.h"
#import "ZXRollView.h"
@interface SportsViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) TopStoriesModel *headerModel;
@property (nonatomic, strong) NSMutableArray *headerArray;



@end

@implementation SportsViewController




- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return  _dataArray;
    
}

- (NSMutableArray *)headerArray {
    if (_headerArray == nil) {
        _headerArray = [NSMutableArray array];
        
    }
    return  _headerArray;
}
- (void)requestData {
    [self.dataArray removeAllObjects];
    [NetWorkRequestManager requestWithType:GET urlString:SportsUrl parDic:nil finish:^(NSData *data) {
        NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = rootDict[@"T1348649079062"];
        for (NSDictionary *dict in arr) {
            TopStoriesModel *model = [[TopStoriesModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            NSArray *adsArray = dict[@"ads"];
            
            //如果ads数组不为空 再进行KVC赋值
            if (adsArray.count > 0) {
                NSMutableArray *adsMArray = [NSMutableArray array];
                for (NSInteger i = 0;i < adsArray.count;i++) {
                    TopStoriesModel *adsModel = [[TopStoriesModel alloc] init];
                    [adsModel setValuesForKeysWithDictionary:adsArray[i]];
                    [adsMArray addObject:adsModel];
                }
                model.ads = adsMArray;
                // 直接初始化model.ads不行
                
            }
            //给imgextra数组赋值
            NSArray *imgextraArray = dict[@"imgextra"];
            if (imgextraArray.count > 0) {
                NSMutableArray *imgextraMArray = [NSMutableArray array];
                for (NSInteger i = 0;i < imgextraArray.count;i++) {
                    TopStoriesModel *imgextraModel = [TopStoriesModel new];
                    [imgextraModel setValuesForKeysWithDictionary:imgextraArray[i]];
                    [imgextraMArray addObject:imgextraModel];
                }
                model.imgextra = imgextraMArray;
            }
            
            
            [self.dataArray addObject:model];
            
        }
        
        // for in 遍历里面不能就行删除操作
        for (NSInteger i = 0;i < self.dataArray.count;i++) {
            TopStoriesModel *model = self.dataArray[i];
            if ([model.skipType  isEqual: @"live"] || [model.skipType isEqualToString:@"video"]) {
                
                [self.dataArray removeObject:model];
            }
            
        }
        
        
        TopStoriesModel *firstModel = self.dataArray[0];
        
        for (NSInteger i = 0;i < firstModel.ads.count; i++) {
            
            NSString *urlStr = firstModel.ads[i].imgsrc;
            [self.headerArray addObject:urlStr];
        }
        self.headerModel = firstModel;
        
        [self.dataArray removeObjectAtIndex:0];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [self initHeaderView];
        });
        
        
    } finishError:^(NSError *error) {
        NSLog(@"头条error%@", error);
    }];
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kwidth, kheight - 44 - 40 - 40) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    [self requestData];
    [self.tableView registerClass:[NormalTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[PhotosetCell class] forCellReuseIdentifier:@"photoCell"];
    
    
    
    
    
    
}
- (void)initHeaderView {
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kwidth, kheight * 3 / 10)];
    [imgView sd_setImageWithURL:[NSURL URLWithString:self.headerModel.imgsrc]];
    self.tableView.tableHeaderView = imgView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count - 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    TopStoriesModel *model = self.dataArray[indexPath.row];
    
    if ([model.skipType isEqualToString:@"photoset"]) {
        PhotosetCell *photoCell = [tableView dequeueReusableCellWithIdentifier:@"photoCell" forIndexPath:indexPath];
        photoCell.model = self.dataArray[indexPath.row];
        return photoCell;
    }
    else  {
        
        NormalTableViewCell *nolmalCell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        nolmalCell.model = self.dataArray[indexPath.row];
        return  nolmalCell;
    }
    
    
    
    return  nil;
    
    
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return  normalCellHeight;
}







@end